package atguigu.aqs;

import java.util.concurrent.locks.Lock;

import org.junit.Test;

public class TwinsLockTest {

    @Test
    public void testTwinsLock() {
        final Lock lock = new TwinsLock();
        class Worker extends Thread {
            @Override
            public void run() {
                while (true) {
                    // ��ȡ��
                    lock.lock();
                    try {
                        SleepUtils.second(1);
                        System.out.println(System.currentTimeMillis() + " " + Thread.currentThread().getName());
                        SleepUtils.second(1);
                    }
                    finally {
                        // �ͷ���
                        lock.unlock();
                    }
                }
            }
        }
        // ��10���߳�����worker, ���û������Ӧ���Ǽ���ͬʱ�ܿ����
        // �� TwinsLock ֻ����ͬʱ�������̻߳��������
        for (int i = 0; i < 10; i++) {
            Worker w = new Worker();
            w.setDaemon(true);
            w.start();
        }
        // ÿ��1s����
        for (int i = 0; i < 10; i++) {
            SleepUtils.second(1);
            System.out.println();
        }

    }
}